package org.boulderfoodrescue.webapp.client.options.manageusers;


import java.util.Comparator;
import java.util.List;

import org.boulderfoodrescue.webapp.client.dialogs.DialogCallback;
import org.boulderfoodrescue.webapp.client.dialogs.confirm.ConfirmDialog;
import org.boulderfoodrescue.webapp.client.managers.UserAccountManager;
import org.boulderfoodrescue.webapp.client.resources.Resources;
import org.boulderfoodrescue.webapp.shared.UserAccountDetails;
import org.boulderfoodrescue.webapp.shared.UserAccountDetails.AccessLevel;

import com.google.gwt.cell.client.ClickableTextCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.ColumnSortEvent.ListHandler;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;

public class UsersCellTable extends FlowPanel{
  protected CellTable<UserAccountDetails> table;
  protected ListDataProvider<UserAccountDetails> dataProvider;
  public UsersCellTable(final EventBus eventBus, List<UserAccountDetails> users){
    dataProvider = new ListDataProvider<UserAccountDetails>();
    table = new CellTable<UserAccountDetails>();

    TextColumn<UserAccountDetails> nameColumn = new TextColumn<UserAccountDetails>(){
      @Override
      public String getValue(UserAccountDetails object) {
        return object.getFirstName();
      }
    };

    TextColumn<UserAccountDetails> nameColumn2 = new TextColumn<UserAccountDetails>(){
      @Override
      public String getValue(UserAccountDetails object) {
        return object.getLastName();
      }
    };

    ClickableTextCell removeCell = new ClickableTextCell();
    Column<UserAccountDetails, String> removeColumn = new Column<UserAccountDetails, String>(removeCell){
      @Override
      public String getValue(UserAccountDetails object) {
        return "Delete User";
      }
    };
    removeColumn.setCellStyleNames(Resources.INSTANCE.ops().textButtonCell());

    TextColumn<UserAccountDetails> accessColumn = new TextColumn<UserAccountDetails>(){
      @Override
      public String getValue(UserAccountDetails object) {
        if (object.getAccessLevel() == AccessLevel.Public)
        {
          return "Public";
        }
        else if (object.getAccessLevel() == AccessLevel.Volunteer)
        {
          return "Volunteer";
        }
        else
          return "Administrator";
      }
    };

    TextColumn<UserAccountDetails> emailColumn = new TextColumn<UserAccountDetails>(){
      @Override
      public String getValue(UserAccountDetails object) {
        return object.getEmail();
      }
    };

    table.addColumn(nameColumn, "First Name");
    table.addColumn(nameColumn2, "Last Name");
    table.addColumn(accessColumn, "User Access");
    table.addColumn(emailColumn, "Email");
    table.addColumn(removeColumn, "Delete");

    dataProvider.addDataDisplay(table);

    ListHandler<UserAccountDetails> sortHandler = new ListHandler<UserAccountDetails>(dataProvider.getList());
    table.addColumnSortHandler(sortHandler);

    nameColumn.setSortable(true);
    sortHandler.setComparator(nameColumn, new Comparator<UserAccountDetails>(){
      @Override
      public int compare(UserAccountDetails o1, UserAccountDetails o2) {
        return o1.getFirstName().compareTo(o2.getFirstName());
      }
    });

    nameColumn2.setSortable(true);
    sortHandler.setComparator(nameColumn, new Comparator<UserAccountDetails>(){
      @Override
      public int compare(UserAccountDetails o1, UserAccountDetails o2) {
        return o1.getLastName().compareTo(o2.getLastName());
      }
    });

    removeColumn.setFieldUpdater(new FieldUpdater<UserAccountDetails, String>(){
      @Override
      public void update(int index, final UserAccountDetails object, String value) {
      	ConfirmDialog dlg = new ConfirmDialog("Are you sure you want to delete user: "+object.getEmail(),
      	    new DialogCallback(){
              @Override
              public void onCancelOperation() {
                // TODO Auto-generated method stub
                
              }
              @Override
              public void onConfirmOperation() {
                UserAccountManager.deleteUser(object);
              }
      	});
      	dlg.center();
      }
    });

    final SingleSelectionModel<UserAccountDetails> selectionModel = new SingleSelectionModel<UserAccountDetails>();
    table.setSelectionModel(selectionModel);
    selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
      @Override
      public void onSelectionChange(SelectionChangeEvent event) {
        //AccountUser selected = selectionModel.getSelectedObject();

      }
    });

    this.add(table);
    table.setWidth("100%");
    
    if(users != null)
    	dataProvider.getList().addAll(users);
  }
  public void addUser(UserAccountDetails new_user){
    dataProvider.getList().add(new_user);
  }
  public void removeUser(UserAccountDetails user){
    dataProvider.getList().remove(user);
  }
  public void refresh()
  {
  	table.redraw();
  }
}
